package cn.itcast.controller;

import cn.itcast.constant.MessageConstant;
import cn.itcast.constant.RedisConstant;
import cn.itcast.domain.Setmeal;
import cn.itcast.entity.PageResult;
import cn.itcast.entity.QueryPageBean;
import cn.itcast.entity.Result;
import cn.itcast.service.SetMealService;
import cn.itcast.utils.QiniuUtils;
import com.alibaba.dubbo.config.annotation.Reference;
import org.apache.commons.io.FilenameUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import redis.clients.jedis.JedisPool;

import java.io.IOException;
import java.util.List;
import java.util.UUID;

@RestController
@RequestMapping("/setMeal")
public class SetMealController {

    @Reference
    private SetMealService setMealService;

    @Autowired
    //远程调用
    private JedisPool jedisPool;

    /**
     * 分页查询检查组信息
     *
     * @param queryPageBean
     * @return
     */
    @RequestMapping("/findByPage")
    public PageResult findByPage(@RequestBody QueryPageBean queryPageBean) {

        PageResult pageResult = setMealService.findByPage(queryPageBean);

        return pageResult;

    }

    /**
     * 上传图片到七牛云 和预览
     *
     * @param imgFile
     * @return
     */
    @RequestMapping("/upload")
    public Result upload(@RequestParam("imgFile") MultipartFile imgFile) {
        //获取文件的名称
        String originalFilename = imgFile.getOriginalFilename();
        //使用工具类 FileNameUtils.getExtension
        String extension = FilenameUtils.getExtension(originalFilename);
        //使用uuid 得到不会重复的文件名称
        String fileName = UUID.randomUUID().toString() + "." + extension;
        try {
            QiniuUtils.upload2Qiniu(imgFile.getBytes(), fileName);
            //将上传的所有图片名称存入Redis，基于Redis的Set集合存储
            //第一次,redis和quartz结合,定时清理垃圾图片
            jedisPool.getResource().sadd(RedisConstant.SETMEAL_PIC_RESOURCES, fileName);
        } catch (IOException e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.PIC_UPLOAD_FAIL);
        }

        return new Result(true, MessageConstant.UPLOAD_SUCCESS, fileName);
    }

    /**
     * 新增检查套餐
     *
     * @param setMeal
     * @param checkgroupIds
     * @return
     */
    @RequestMapping("/add")
    public Result insertSetMail(@RequestBody Setmeal setMeal, Integer[] checkgroupIds) {

        try {
            setMealService.insertSetMail(setMeal, checkgroupIds);

        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.ADD_SETMEAL_FAIL);
        }
        return new Result(true, MessageConstant.ADD_SETMEAL_SUCCESS);
    }

    /**
     * 查询检查套餐的基本信息
     *
     * @param id
     * @return
     */
    @RequestMapping("/findSetMeal")
    public Result findSetMeal(Integer id) {
        Setmeal setmeal = null;
        try {
            setmeal = setMealService.findSetMeal(id);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_SETMEAL_FAIL);
        }
        return new Result(true, MessageConstant.QUERY_SETMEAL_SUCCESS, setmeal);
    }

    /**
     * 修改
     *
     * @param setmeal
     * @param checkgroupIds
     * @return
     */
    @RequestMapping("/update")
    public Result update(@RequestBody Setmeal setmeal, Integer[] checkgroupIds) {

        try {
            setMealService.update(setmeal, checkgroupIds);

        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.EDIT_SETMEAL_FAIL);
        }
        return new Result(true, MessageConstant.EDIT_SETMEAL_SUCCESS);
    }

    /**
     * 图片的数据回写
     *
     * @param id
     * @return
     */
    @RequestMapping("/findImg")
    public String findImg(Integer id) {
        //做判断,数据库中的图片如果在七牛云中,不存在如何解决
        String img = setMealService.findSetMeal(id).getImg();
        return img;
    }

    /**
     * 查询检查套餐对应的检查组的ids
     *
     * @param id
     * @return
     */
    @RequestMapping("/findCheckGroupIds")
    public Result findCheckGroupIds(Integer id) {
        List<Integer> list = null;
        try {
            list = setMealService.findCheckGroupIds(id);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_CHECKGROUP_FAIL);
        }
        return new Result(true, MessageConstant.QUERY_CHECKGROUP_SUCCESS, list);
    }

    /**
     * 删除
     *
     * @return
     */
    @RequestMapping("/delete")
    public Result delete(Integer id) {
        try {
            setMealService.delete(id);

        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.DELETE_SETMEAL_SUCCESS);
        }
        return new Result(true, MessageConstant.DELETE_SETMEAL_SUCCESS);
    }
}
